/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.babble.manage.controller;

import com.babble.manage.entity.Img;
import com.babble.manage.service.IImgService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springblade.common.entity.MinioPolicy;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 图片表 控制器
 *
 * @author Blade
 * @since 2021-11-21
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("img")
@Api(value = "图片模块", tags = "图片模块")
public class ImgController extends BladeController {
	private final IImgService imgService;

	@PostMapping("upload")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "上传图片", notes = "上传图片保存到oss里")
	public R<Img> upload(@RequestParam("pictureSortId") @NotEmpty(message = "不能为空") String pictureSortId, @RequestParam("file") MultipartFile imgFile) {
		Img img = imgService.upload(pictureSortId, imgFile);
		if (img == null) {
			return R.fail("上传失败");
		}
		return R.data(img);
	}

	@GetMapping("policy")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取策略", notes = "获取策略直传图片")
	public R<MinioPolicy> getPolicy(@RequestParam @NotEmpty(message = "不能为空") String pictureSortId, @RequestParam @NotEmpty(message = "不能为空") String fileName, @RequestParam @NotEmpty(message = "不能为空") String contentType) {
		return R.data(imgService.getPolicy(pictureSortId, fileName, contentType));
	}

	@GetMapping("{id}/category-list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分类图片", notes = "获取分页分类图片")
	public R<IPage<Img>> list(@PathVariable("id") String id, Query query) {
		return R.data(imgService.list(id, query));
	}

	@DeleteMapping("{id}")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "删除图片", notes = "删除图片")
	public R delete(@PathVariable("id") String id) {
		imgService.delete(id);
		return R.success("删除成功");
	}

	@DeleteMapping("deletes")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "批量删除", notes = "批量删除图片")
	public R delete(@RequestBody List<String> ids) {
		imgService.deletes(ids);
		return R.success("删除成功");
	}
}
